package com.chaopei.znpk.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.chaopei.znpk.entity.Course;
import com.chaopei.znpk.service.CourseService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @Author Bin
 * @Date 2022/5/26 11:16
 */
@RestController
@RequestMapping("/Course")
@CrossOrigin
public class CourseController {

    @Resource
    private CourseService courseService;

    /**
     * 下载模板
     * @param response
     * @throws IOException
     */
    @GetMapping("/downLoadTemplate")
    public void downLoadTemplate(HttpServletResponse response) throws IOException {
        List<Object> list = null;
        ExcelWriter writer = null;
        OutputStream outputStream = response.getOutputStream();
        try {
            //添加响应头信息
            response.setHeader("Content-disposition", "attachment; filename=" + "课程模板.xlsx");
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma", "No-cache");//设置头
            response.setHeader("Cache-Control", "no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            EasyExcel.write(outputStream, Course.class).sheet("课程模板").doWrite(list);
            writer.finish();
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 上传课程信息
     * @param file
     * @param request
     * @return
     * @throws IOException
     */
    @PostMapping("/upLoadExcel")
    public List<Course> upLoadExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
        List<Course> excel = EasyExcel.read(file.getInputStream(), Course.class, new AnalysisEventListener<Course>() {

            @Override
            public void invoke(Course o, AnalysisContext analysisContext) {
                System.out.println(o);
            }

            @Override
            public void doAfterAllAnalysed(AnalysisContext analysisContext) {

            }
        } ).sheet(0).doReadSync();
        System.out.println(excel);
        //进行数据库插入操作
        for (Course course : excel) {
            courseService.add(course);
        }
        return excel;
    }

    /**
     * 下载课程信息
     * @param courses
     * @param response
     * @throws IOException
     */
    @GetMapping("/downLoadCourseInfos")
    public void downLoadErrorInfos(@RequestBody List<Course> courses,HttpServletResponse response) throws IOException{
        ExcelWriter writer = null;
        OutputStream outputStream = response.getOutputStream();
        try {
            //添加响应头信息
            response.setHeader("Content-disposition", "attachment; filename=" + "课程信息.xlsx");
            response.setContentType("application/msexcel;charset=UTF-8");//设置类型
            response.setHeader("Pragma", "No-cache");//设置头
            response.setHeader("Cache-Control", "no-cache");//设置头
            response.setDateHeader("Expires", 0);//设置日期头
            EasyExcel.write(outputStream, Course.class).sheet("课程信息").doWrite(courses);
            writer.finish();
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                response.getOutputStream().close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    /**
     * 插入单条课程信息
     * @param course
     */
    @PostMapping("/save")
    public Course save(@RequestBody Course course){
        int i = courseService.add(course);
        if (i>0){
            //成功插入数据
            return course;
        }
        //插入失败
        return null;
    }

    /**
     * 获取所有课程信息数据
     * @return
     */
    @GetMapping("/list")
    public List<Course> list(){
        courseService.list().forEach(System.out::println);
        return courseService.list();
    }
}
